package threadLocal;

import java.util.HashMap;
import java.util.Map;

/*
* 每个线程都有一个ThreadLocalMap对象，ThreadLocalMap是Thread的一个内部类，可以把ThreadLocalMap理解成一个map，map中放着一个Thread的所有线程变量。
* */
public class MyThreadLocal {
    private static final ThreadLocal<Object> threadLocal = new ThreadLocal<Object>(){
        @Override
        protected Object initialValue(){
            System.out.println("调用get方法时，当前线程共享变量没有设置，调用initialValue获取默认值");
            return null;
        }
    };

    public static void main(String[] args){
        new Thread(new MyStringTask("StringTask1")).start();
        new Thread(new MyStringTask("StringTask2")).start();
        new Thread(new MyIntegerTask("IntegerTask1")).start();
        new Thread(new MyIntegerTask("IntegerTask2")).start();
    }

    public static class MyIntegerTask implements Runnable{
        Map<String,Object> map = new HashMap<String,Object>();
        private String name;
        MyIntegerTask(String name){
            this.name = name;
        }
        @Override
        public void run(){
            for (int i=0; i<5;i++){
                if (null == MyThreadLocal.threadLocal.get()){
                    MyThreadLocal.threadLocal.set(0);
                    System.out.println("线程"+name+":0");
                }else {
                    int num = (Integer) MyThreadLocal.threadLocal.get();
                    MyThreadLocal.threadLocal.set(num+1);
                    System.out.println("线程"+name+":"+MyThreadLocal.threadLocal.get());
                    if(i==3){
                        MyThreadLocal.threadLocal.remove();
                    }
                }
                try{
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static class MyStringTask implements Runnable{
        private String name;
        MyStringTask(String name){
            this.name = name;
        }

        @Override
        public void run() {
            for (int i=0; i < 5; i++){
                if(null == MyThreadLocal.threadLocal.get()){
                    MyThreadLocal.threadLocal.set("a");
                    System.out.println("线程"+name+":a");
                }else{
                    String str = (String) MyThreadLocal.threadLocal.get();
                    MyThreadLocal.threadLocal.set(str + "a");
                    System.out.println("线程" + name + ":" + MyThreadLocal.threadLocal.get());
                }
                try{
                    Thread.sleep(800);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
